<?php 

/**
 * 商品管理控制器
 * @author 阿柱 <422852954@qq.com>
 */
class GoodsController extends CommonController{
    /**
     *   **列表展示方法
     */
    public function index(){
    	$this->topCate();

    	$gid=Q('get.gid',0,'intval');
//		每进一次商品页面点击数加1
		K('Goods')->inc('click',"gid={$gid}",1);		
		$goods=M()->join('__goods__ g JOIN __goods_details__ gd ON g.gid=gd.gid')->where("g.gid={$gid}")->find();
//		p($goods);
//		总库存
		$sum=K('GoodsList')->where("gid={$gid}")->sum('inventory');
//		(规格atype=1)
		$specData=M()->join('__goods__ g JOIN __type_attr__ ta ON g.tid=ta.tid')->where("ta.atype=1 AND g.gid={$gid}")->all();
//		给数组绑定一个规格字段
		foreach ($specData as $k => $v) {
			$where="taid=".$v['taid']." AND gid={$gid}";
			$specData[$k]['spec']=K('GoodsAttr')->where("{$where}")->all();
//			汉字转英文
			$specData[$k]['name']=string::pinyin($specData[$k]['aname']);
		}
//		分配规格
		$this->assign('specData',$specData);
//		追加总库存字段
		$goods['sum']=$sum;
		$this->assign('goods',$goods);
	//		最爱排行	
		$favorite=S('favorite');
		if(!$favorite){
			$favorite=M()->join('__goods__ g JOIN __goods_details__ gd ON g.gid=gd.gid')->order('rand()')->limit('5')->field(array('g.gid','small','gname','shopprice'))->all();
			S('favorite',$favorite,3600);	
		}
		$this->assign('favorite',$favorite);
//		热销排行
		$hot=S('hot');
		if(!$hot){
			$hot=M()->join('__goods__ g JOIN __goods_details__ gd ON g.gid=gd.gid')->order('click DESC')->limit('5')->field(array('g.gid','small','gname','shopprice'))->all();
			S('hot',$hot,3600);
		}	
		$this->assign('hot',$hot);
//		店主推荐
		$recommond=K('Goods')->order('rand()')->limit('4')->all();
		$this->assign('recommond',$recommond);
//		加入购物车的个数宝贝
        $this->display(); 
    }
	
	/**
	 * 异步查询库存价格
	 */
	public function ajaxQuery(){
		if(!IS_AJAX)   return;
//		接收gaids去掉右边的,
		$gaids=rtrim(Q('post.gaids',''),',');
		$arr=explode(',', $gaids);
//		声明空数组
		$add=array();
//		查询附加价格
		foreach ($arr as $v) {
			$add[]=K('GoodsAttr')->where("gaid={$v}")->find();
		}
		$oldPrice=K('Goods')->where("gid={$add[0]['gid']}")->getField('shopprice');
//		原价
		$data['add']=$oldPrice;
//		附加价格
		foreach ($add as $k => $v) {
			$data['add']+=$v['add'];
		}
//		总价格取两位小数
		$data['add']=sprintf("%1\$.2f",$data['add']);		
//		库存*************		
		$inven=K('GoodsList')->where("gid={$add[0]['gid']} AND combine like '%{$gaids}%' ")->all();
		static $sum=0;
		foreach ($inven as $v) {
			$sum+=$v['inventory'];
		}
		$data['sum']=$sum;		
		$this->ajax($data);
	}
	
	/**
	 * 异步加入购物车
	 */
	public function addCart(){
		if(!IS_AJAX)   return;
		
		$gaids=rtrim(Q('post.gaids',''),',');
		
		$data=array(
			'id'=>K('GoodsList')->where("combine='{$gaids}'")->getField('glid'),
			'name'=>K('Goods')->where("gid={$_POST['gid']}")->getField('gname'),
			'num'=>Q('post.num',0),
			'price'=>Q('post.price',0),
			'inventory'=>Q('post.inventory',0,'intval'),
			'pic'=>K('GoodsDetails')->where("gid={$_POST['gid']}")->getField('small'),
		);
//		如果该商品有规格
		if(Q('post.gids')){
			$arr=explode(',', $gaids);
			foreach ($arr as $gaid) {
				$spec=M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.taid')->where("gaid={$gaid}")->getField('aname');
				$data[String::pinyin($spec)]=K('GoodsAttr')->where("gaid={$gaid}")->getField('gvalue');
			}	
		}
		Cart::add($data);
		$phpData=array(
			'num'=> count(Cart::getGoods()),
			'count'=> Cart::getTotalPrice(),
			'totalNum'=>Cart::getTotalNums()
		);
		$this->ajax($phpData);
		
	}
	
}
 

 
 
 
 
 
 







 ?>